<!DOCTYPE html>
<html lang="en">
<head>
    <title>AGV调度演示</title>
	<meta charset="utf-8">
	<meta http-equiv="content-type" content="text/html;utf-8" />
	<meta name="keywords" content="agv" />
	<meta name="description" content="AGV" />
	<meta name="author" content="ttscm.com.cn" />
	<meta name="viewport" content="width=device-width, initial-scale=1.0" />
	<meta name="format-detection" content="telephone=no" />
	<script src="/srds/js/jquery-3.6.0.min.js"></script>

	<script>
	// 1. 小车取料到上料待命点
	// 2. 查询机械手状态，上料并回返程待命点
	// 3. 查询机械手状态，通知小车拉空蛋车返程
	// 第一个寄存器AGV控制，1-AGV上料完成，2-空车拉回完成；第二个寄存器机械臂控制，1-要求AGV上料，2-要求AVG拉回空车；

		var statusInterval = null;
		var signalInterval = null;
		
		var isAgvCompleted = false;
		//const AGV_PARKING_LOCATION_MARK = "LM15";
		//const TASK_LIST_NAME_01 = "TL_ZFLKM_01";
		//const TASK_LIST_NAME_02 = "TL_ZFLKM_02";
		
		const AGV_UP_PARKING_LOCATION_MARK = "LM5"; //上料待命点
		const AGV_TAKE_PARKING_LOCATION_MARK = "LM7"; //机械臂取料待命点
		const AGV_BACK_PARKING_LOCATION_MARK = "AP10"; //机械臂取料待命点
		
		const TASK_LIST_NAME = "F1"; // 导航到上料待命点
		const TASK_LIST_NAME_UP = "F2"; // 上料到机械臂
		const TASK_LIST_NAME_BACK = "Q"; // 空蛋车返程

		$(function() {

			//智飞龙科马
			$("#btnZflkm").click(function() {
				execTask();
			});
			
		});


		// 执行任务
		function execTask() {
			$("#areaExecuteStatus").css("color", "#000000");
			$("#areaExecuteStatus").text("正在下发调度指令...");
			let strUrl = "/srds/zflkm/execTask/" + TASK_LIST_NAME; //1. 导航到上料待命点
			$.getJSON(strUrl, function(result) {
				if (result.code != 0) {
					$("#areaExecuteStatus").css("color", "#FF0000");
					$("#areaExecuteStatus").text("异常代码：" + result.code + "\n\n异常消息：" + result.message);
					return;
				}
				$("#areaExecuteStatus").css("color", "#000000");
				$("#areaExecuteStatus").text("调度指令下发正常...");
				
				// 查询上料导航状态
				statusInterval = setInterval(readUpParkingStatus, 1000);
			});
		}
		
		// 查询上料导航状态
		function readUpParkingStatus() {
			$("#areaExecuteStatus").css("color", "#000000");
			$("#areaExecuteStatus").text("任务执行中..." + new Date().getTime());

			$.getJSON("/srds/zflkm/taskStatus", function(result) {
				if (result.code != 0) {
					clearInterval(statusInterval);
					console.error(result);
					$("#areaExecuteStatus").css("color", "#FF0000");
					$("#areaExecuteStatus").text("异常代码：" + result.code + "\n\n异常消息：" + result.message);
					return;
				}
				if (result.data.statusCode == 4 && result.data.targetId == AGV_UP_PARKING_LOCATION_MARK) {
					clearInterval(statusInterval);
					$("#areaExecuteStatus").css("color", "#000000");
					$("#areaExecuteStatus").text("AGV已到上料待命点...");
					console.info("AGV已到上料待命点...");
					
					// 查询机械臂可用状态
					signalInterval = setInterval(readDevAva, 1000);
				};
			});
		}

		// 查询机械臂可用状态
		function readDevAva() {
			// 2. 查询机械手状态，上料并回返程待命点
			
			// 查询机械臂可用状态 0-初始；1-机械臂可用；2-AGV上料完成；3-机械臂取料完成；
			let strUrl = "/srds/zflkm/readSignal/";
			$.getJSON(strUrl, function(result) {
				if (result.code != 0) {
					$("#areaExecuteStatus").css("color", "#FF0000");
					$("#areaExecuteStatus").text("异常代码：" + result.code + "\n\n异常消息：" + result.message);
					clearInterval(signalInterval);
					return;
				}
				let iSignal = result.data;
				console.info("机械臂状态", iSignal);
				
				if (iSignal == 1) {	// 机械手可用，AGV上料
					$("#areaExecuteStatus").css("color", "#000000");
					$("#areaExecuteStatus").text("机械手可用，呼叫AGV上料...");
					clearInterval(signalInterval);

					$("#areaExecuteStatus").css("color", "#000000");
					$("#areaExecuteStatus").text("正在下发调度指令...");
					let strUrl = "/srds/zflkm/execTask/" + TASK_LIST_NAME_UP; //上料到机械臂
					$.getJSON(strUrl, function(result) {
						if (result.code != 0) {
							$("#areaExecuteStatus").css("color", "#FF0000");
							$("#areaExecuteStatus").text("异常代码：" + result.code + "\n\n异常消息：" + result.message);
							return;
						}
						$("#areaExecuteStatus").css("color", "#000000");
						$("#areaExecuteStatus").text("调度指令下发正常...");
						
						// 查询上料到机械臂状态
						statusInterval = setInterval(readUpDevStatus, 1000);
					});
				}
			});
		}
		
		// 查询上料到机械臂状态
		function readUpDevStatus() {
			$("#areaExecuteStatus").css("color", "#000000");
			$("#areaExecuteStatus").text("任务执行中..." + new Date().getTime());

			$.getJSON("/srds/zflkm/taskStatus", function(result) {
				if (result.code != 0) {
					clearInterval(statusInterval);
					$("#areaExecuteStatus").css("color", "#FF0000");
					$("#areaExecuteStatus").text("异常代码：" + result.code + "\n\n异常消息：" + result.message);
					return;
				}
				if (result.data.statusCode == 4 && result.data.targetId == AGV_TAKE_PARKING_LOCATION_MARK) {
					clearInterval(statusInterval);
					$("#areaExecuteStatus").css("color", "#000000");
					$("#areaExecuteStatus").text("AGV上料完毕...");
					console.info("AGV上料完毕...");
					
					// 通知机械臂取 0-初始；1-机械臂可用；2-AGV上料完成；3-机械臂取料完成；
					noticeDev();
				};
			});
		}


		// 通知机械臂取料
		function noticeDev() {
			$("#areaExecuteStatus").css("color", "#000000");
			$("#areaExecuteStatus").text("正在发送信号...");
			let strUrl = "/srds/zflkm/setSignal/";
			$.getJSON(strUrl, function(result) {
				if (result.code != 0) {
					$("#areaExecuteStatus").css("color", "#FF0000");
					$("#areaExecuteStatus").text("异常代码：" + result.code + "\n\n异常消息：" + result.message);
					return;
				}
				$("#areaExecuteStatus").css("color", "#000000");
				$("#areaExecuteStatus").text("发送信号正常...");
				
				console.info("---信号已发送，等待设备完成...");
				
				signalInterval = setInterval(readDevTakeStatus, 1000);
			});
		}

		// 查询机械臂取料完成状态
		function readDevTakeStatus() {
			// 查询机械臂取料完成状态 0-初始；1-机械臂可用；2-AGV上料完成；3-机械臂取料完成；
			let strUrl = "/srds/zflkm/readSignal/";
			$.getJSON(strUrl, function(result) {
				if (result.code != 0) {
					$("#areaExecuteStatus").css("color", "#FF0000");
					$("#areaExecuteStatus").text("异常代码：" + result.code + "\n\n异常消息：" + result.message);
					clearInterval(signalInterval);
					return;
				}
				let iSignal = result.data;
				console.info("机械臂状态", iSignal);
				
				if (iSignal == 3) {	// 机械臂取料完毕
					$("#areaExecuteStatus").css("color", "#000000");
					$("#areaExecuteStatus").text("机械臂取料完毕，呼叫AGV返程...");
					clearInterval(signalInterval);

					$("#areaExecuteStatus").css("color", "#000000");
					$("#areaExecuteStatus").text("正在下发调度指令...");
					let strUrl = "/srds/zflkm/execTask/" + TASK_LIST_NAME_BACK; //空蛋车返程
					$.getJSON(strUrl, function(result) {
						if (result.code != 0) {
							$("#areaExecuteStatus").css("color", "#FF0000");
							$("#areaExecuteStatus").text("异常代码：" + result.code + "\n\n异常消息：" + result.message);
							return;
						}
						$("#areaExecuteStatus").css("color", "#000000");
						$("#areaExecuteStatus").text("调度指令下发正常...");
						
						// 查询AGV返程状态，重置设备状态为0
						statusInterval = setInterval(readBackStatus, 1000);
					});
				}
			});
		}

		// 查询返程导航状态
		function readBackStatus() {
			$("#areaExecuteStatus").css("color", "#000000");
			$("#areaExecuteStatus").text("任务执行中..." + new Date().getTime());

			$.getJSON("/srds/zflkm/taskStatus", function(result) {
				if (result.code != 0) {
					clearInterval(statusInterval);
					console.error(result);
					$("#areaExecuteStatus").css("color", "#FF0000");
					$("#areaExecuteStatus").text("异常代码：" + result.code + "\n\n异常消息：" + result.message);
					return;
				}
				if (result.data.statusCode == 4 && result.data.targetId == AGV_BACK_PARKING_LOCATION_MARK) {
					clearInterval(statusInterval);
					$("#areaExecuteStatus").css("color", "#000000");
					$("#areaExecuteStatus").text("AGV已返程...");
					console.info("AGV已返程，重置机械臂状态...");
					
					resetSignal();
				};
			});
		}		

		// 重置信号
		function resetSignal() {
			$("#areaExecuteStatus").css("color", "#000000");
			$("#areaExecuteStatus").text("正在重置信号...");
			let strUrl = "/srds/zflkm/resetSignal/";
			$.getJSON(strUrl, function(result) {
				if (result.code != 0) {
					$("#areaExecuteStatus").css("color", "#FF0000");
					$("#areaExecuteStatus").text("异常代码：" + result.code + "\n\n异常消息：" + result.message);
					return;
				}
				$("#areaExecuteStatus").css("color", "#000000");
				$("#areaExecuteStatus").text("重置信号正常...任务完成");
			});
		}
		
  </script>
</head>
<body style="font-size:100%;">
	<div id="container" style="border:0px solid #FF0000;margin:0px;padding:1px;">
		<div id="header" style="border:0px solid #00FF00;background-color:#C0C0C0;text-align:center;font-size:200%;margin:5px;padding:1px;">
			<b>调度演示</b>
		</div>

		<div id="content" style="border:0px solid #0000FF;background-color:#EEEEEE;margin:5px;padding:1px;">	
			<table style="width:100%;">
				<tr>
					<td style="width:30%;"><label for="txtCurrSite">当前站点：</label></td>
					<td style="width:70%;"><input id="txtCurrSite" type="text" style="width:95%;height:25px;" disabled="disabled" /></td>
				</tr>
				<tr>
					<td colspan="2" style="text-align:center;height:80px;">
						<button id="btnZflkm" type="button" style="width:100%;height:50px;background-color:red;">定点-上料-卸料</button>
					</td>
				</tr>
				<tr>
					<td>
						<label for="areaExecuteStatus" >执行状态：</label>
					</td>
					<td style="text-align:center;">
						<textarea id="areaExecuteStatus" style="width:95%;min-height:400px;font-size:150;font-weight:bold;color:#000000%" disabled="disabled"></textarea>
					</td>
				</tr>
			</table>
		</div>

		<div id="footer" style="border:0px solid #00FF00;background-color:#C0C0C0;text-align:center;margin:5px;padding:1px;">
			版权 &#169 天天供应链 &#8226 天天智能科技
		</div>
	</div>
</body>
</html>